package Page.TeachingInfoPage;

import Navigation.NavigationController;
import DataClass.Course;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;

import java.util.ArrayList;
import java.util.List;

public class TeachingInfoViewModel {
    ListProperty<Course> dataList = new SimpleListProperty<>(FXCollections.observableArrayList());
    // 列表副本
    private List<Course> TeachingInfoCopy = new ArrayList<Course>();

    private final StringProperty inputKeyWord = new SimpleStringProperty();

    public ListProperty<Course> getDataListProperty() {
        return dataList;
    }
    public ObservableList<Course> getDataList() {
        return dataList.get();
    }

    public void setDataList(ObservableList<Course> dataList) {
        this.dataList.set(dataList);
    }

    public void setDataList(List<Course> dataList) {

        this.TeachingInfoCopy = dataList;
        setDataList(FXCollections.observableArrayList(dataList));
    }

    public void backButtonClicked(){
        NavigationController.back();
    }

    public StringProperty getInputKeyWord(){
        return inputKeyWord;
    }
    public void updateInputKeyWord(String string)
    {
        inputKeyWord.set(string);
    }

    // 根据用户输入过滤
    public void clickFindButton(){
        String targetKeyWord = inputKeyWord.get();

        if(targetKeyWord != null && !targetKeyWord.isEmpty()) {
            FilteredList<Course> filteredData = new FilteredList<>(
                    FXCollections.observableArrayList(TeachingInfoCopy),
                    p -> p.getCname().contains(targetKeyWord)
            );
            dataList.setAll(filteredData);
        }
    }

    // 清空过滤
    public void clearFilter(){
        this.inputKeyWord.set(null);
        this.setDataList(TeachingInfoCopy);
    }
    public void refresh(){
        this.inputKeyWord.set(null);
    }
}
